package com.itheima.d2_recursion;

/**
 * 递归
 */
public class RecursionDemo1 {
    public static void main(String[] args) {
        //1.求 5 的阶乘
        int num1 = f(5);
        System.out.println(num1);

        //2.求1-n 的累加和
        int num2 = ff(10);
        System.out.println(num2);

    }

    private static int ff(int n) {
        //1.ff(n) = ff(n-1) + n;
        //2.终结点：f(1) == 1
        if(n == 1){
            return 1;
        }else{
            return ff(n - 1) + n;
        }
    }

    //递归三要素:1.递归公式 f(n) = f(n-1) * n;
    //         2.终结点 f(1) == 1
    //         3.递归走向终结点
    private static int f(int i) {
        if(i == 1){
            return 1;
        }else{
            return f(i - 1) * i;
        }
    }
}
